package com.hnevc.xiaoming.httpactivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.hnevc.xiaoming.R;
import com.hnevc.xiaoming.adapter.NewsAdapter;
import com.hnevc.xiaoming.httpbean.NewsBean;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyNewsActivity extends AppCompatActivity {


    private ImageView back;
    private TextView title_view;
    private ListView lv_news;
    private List<NewsBean.ResultBean.DataBean> list;
    private String key;
    private String type;
    private String url;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_layout);
        initView();
        initDate();
        initEvent();


    }

    private void initEvent() {

        lv_news.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                NewsBean.ResultBean.DataBean bean = list.get(position);
                Toast.makeText(MyNewsActivity.this,""+bean.getAuthor_name(),Toast.LENGTH_LONG).show();
                Intent intent = new Intent();
                intent.setClass(MyNewsActivity.this,MyNewsContentActivity.class);
                intent.putExtra("content",bean.getUrl()+"");
                startActivity(intent);

            }
        });

    }

    private void initDate() {

//        url = "http://api.tianapi.com/esports/index?key=faab7671fb56d41bc6991be76b94dbdc&num=10";
        url = "http://v.juhe.cn/toutiao/index?type=top&key=368cf096b8c3c4b53dbc664c686b3a6d";

        JSONObject jsonObject = new JSONObject();
//        http://v.juhe.cn/toutiao/index?type=top&key=368cf096b8c3c4b53dbc664c686b3a6d
//        type = "top";
//        key="faab7671fb56d41bc6991be76b94dbdc";
//        int num=10;
//        try {
//            jsonObject.put("key",key);
//            jsonObject.put("num", num);
//            jsonObject.put("tpage",1);
//            jsonObject.put("tpage_size",30);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
        RequestQueue requestQueue = Volley.newRequestQueue(MyNewsActivity.this);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url,
                "", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                Log.e("TAG", "onResponse: "+jsonObject.toString() );
                Gson gson = new Gson();
                NewsBean dataBean = gson.fromJson(jsonObject.toString(), NewsBean.class);
                list.addAll(dataBean.getResult().getData());
                Log.e("TAG", "onResponse: "+list.toString() );
                NewsAdapter newsAdapter = new NewsAdapter(list,MyNewsActivity.this);
                lv_news.setAdapter(newsAdapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MyNewsActivity.this, "网络请求失败！", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);

    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title_view = (TextView) findViewById(R.id.title_view);
        title_view.setText("新闻头条");
        lv_news = (ListView) findViewById(R.id.lv_news);
        list = new ArrayList<>();
    }
}
